package com.tcc.etec.mecashow.util;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class Utils {
	
	public static Bitmap getBackgroundImage(final Resources res, final int resId, final int reqWidth, final int reqHeight) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, resId, options);

        options.inSampleSize = getInSampleSize(options.outWidth, options.outHeight, reqWidth, reqHeight);

        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(res, resId, options);
    }

    private static int getInSampleSize(final int imageWidth, final int imageHeight, final int reqWidth, final int reqHeight) {
        int inSampleSize = 1;

        if (imageHeight > reqHeight || imageWidth > reqWidth) {
            if (imageWidth > imageHeight) {
                inSampleSize = Math.round((float) imageHeight / (float) reqHeight);
            } else {
                inSampleSize = Math.round((float) imageWidth / (float) reqWidth);
            }
        }

        return inSampleSize;
    }
}
